#ifndef CHINACTPREALTIME_H
#define CHINACTPREALTIME_H

#include <QObject>
#include "chinactp/ThostFtdcMdApi.h"

// 行情回调类（继承 API 的接口类）
class ChinactpRealtime : public QObject, public CThostFtdcMdSpi {
    Q_OBJECT

public:
    explicit ChinactpRealtime(CThostFtdcMdApi *api, QObject *parent = nullptr);

    // 连接服务器响应
    void OnSetInstrument(QStringList instrumentList);
    QStringList OnGetInstrument();

    // 连接服务器响应
    void OnFrontConnected() override;

    // 断开连接通知
    void OnFrontDisconnected(int nReason) override;

    // 登录响应
    void OnRspUserLogin(CThostFtdcRspUserLoginField *pRspUserLogin,
                        CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) override;

    // 行情订阅响应
    void OnRspSubMarketData(CThostFtdcSpecificInstrumentField *pSpecificInstrument,
                            CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) override;

    // 深度行情通知（核心回调）
    void OnRtnDepthMarketData(CThostFtdcDepthMarketDataField *pDepthMarketData) override;

    // 错误信息通知
    void OnRspError(CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) override;

signals:
    // 自定义信号，用于向 Qt 主线程传递数据
    void connected();
    void disconnected(int reason);
    void loginResult(bool success, const QString &msg);
    void marketDataReceived(const QString &instrumentID, double lastPrice, double askPrice,double bidPrice); // 行情数据信号

private:
    CThostFtdcMdApi *m_api;  // API 实例

    TThostFtdcBrokerIDType gBrokerID = "";         // 模拟经纪商代码
    TThostFtdcInvestorIDType gInvesterID = "";     // 投资者账户名
    TThostFtdcPasswordType gInvesterPassword = ""; // 投资者密码
    QStringList instrumentList;
};

#endif // CHINACTPREALTIME_H
